import {Express} from "express";
import {ProjectScanner} from "../scanner";
import {globalConfig} from "../config";

export function ProjectService(app: Express) {

  const prefix = '/api/project';

  // 扫描项目
  app.get(prefix + '/scan', async (req, res) => {
    const config = globalConfig.config;
    try {
      const scanResult = await ProjectScanner.scanProject(config.scan.targetDir, {
        depth: config.scan.depth,
        excludes: config.scan.excludes,
        projectTypes: config.scan.projectTypes
      });

      res.json(scanResult);
    } catch (error) {
      console.error('获取项目列表失败:', error);
      res.status(500).json({error: `服务器错误: ${(error as Error).message}`});
    }
  });

  // 获取特定项目详情
  app.get(prefix + ':path', async (req, res) => {
    try {
      const decodedPath = decodeURIComponent(req.params.path);
      const detail = await ProjectScanner.getProjectDetail(decodedPath);
      res.json(detail);
    } catch (error) {
      console.error('获取项目详情失败:', error);
      res.status(500).json({error: `服务器错误: ${(error as Error).message}`});
    }
  });
}
